#define USE_MULTI_THREAD true

#define TEST_WRITE_JOURNAL false

#define USE_NEW_FRAME true

#include <thread>

#if USE_NEW_FRAME
#include "LoggerService.hpp"
#else
#include "DbusService.hpp"
#endif

#include "LogCollection.hpp"

using LogCollector::Severity;


int main(int argc, char const *argv[])
{
    LogCollector::init();

#if USE_NEW_FRAME

    LoggerService service(
        LoggerService::SERVICE_NAME,
        LoggerService::SERVICE_PATH,
        LoggerService::SERVICE_INTERFACE
    );

    service.run();

#else

    DbusService service;
    if (!service.init()) {
        return EXIT_FAILURE;
    }

    SS_LOG(Severity::Level::DEBUG, "service init successfully.");

#if USE_MULTI_THREAD
    //! Draft: We can use a new thread to manager eventloop
    auto thread = std::thread([&service]() {
        service.eventLoop();
    });

#if TEST_WRITE_JOURNAL
    //! for test write to journal.
    for (size_t i = 0; i < 100; i++)
    {
        SS_LOG(Severity::Level::DEBUG, "Output number: %lu", static_cast<unsigned long>(i));
        usleep(1000000);  //! 1 sec
    }
#endif

    if (thread.joinable()) {
        thread.join();
    }
#else
    if (!service.eventLoop()) {
        return EXIT_FAILURE;
    }
#endif

#endif

    SS_LOG(Severity::Level::DEBUG, "service eventLoop stop.");

    LogCollector::deInit();

    return EXIT_SUCCESS;
}
